<?php
// +----------------------------------------------------------------------
// | CoreThink [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2014 http://www.corethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: jry <598821125@qq.com> <http://www.corethink.cn>
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Think\Controller;
/**
 * 后台公共控制器
 * 为防止多分组Controller名称冲突，公共Controller名称统一使用分组名称
 * @author jry <598821125@qq.com>
 */
class AdminController extends Controller{
    /**
     * 初始化方法
     * @author jry <598821125@qq.com>
     */
    protected function _initialize(){
        //登录检测
        if(!is_login()){ //还没登录跳转到登录页面
            $this->redirect('Public/login');
        }

        //读取数据库中的配置
        $config = S('DB_CONFIG_DATA');
        if(!$config){
            $config = D('Config')->lists();
            $config['DEFAULT_THEME'] = ''; //后台无模板主题
            S('DB_CONFIG_DATA',$config);
        }
        C($config); //添加配置

        //获取菜单导航
        $current_menu = D('menu')->getMenuByControllerAndAction(); //获取当前菜单
        $main_menu = D('menu')->getMainMenuById($current_menu['id']); //获取当前菜单的顶级菜单
        $side_menu = D('menu')->getSideMenuByMainMenuId($main_menu['id']); //获取当前顶级菜单的子菜单作为侧边导航菜单

        $this->assign('__MAINMENU__', D('menu')->getMenuByPid()); //主菜单导航
        $this->assign('__SIDEMENU__', $side_menu); //侧边菜单导航
        $this->assign('__CURRENT_MAINMENU__', $main_menu['id']);
        $this->assign('__CURRENT_SIDEMENU__', $current_menu['id']);
    }

    /**
     * 设置一条或者多条数据的状态
     * @author jry <598821125@qq.com>
     */
    public function setStatus($model = CONTROLLER_NAME){
        $ids    = I('request.ids');
        $status = I('request.status');
        if(empty($ids)){
            $this->error('请选择要操作的数据');
        }
        $map['id'] = array('in',$ids);
        switch($status){
            case 'forbid'  : //禁用条目
                $data = array('status' => 0);
                $this->editRow($model, $data, $map, array('success'=>'禁用成功','error'=>'禁用失败'));
                break;
            case 'resume'  : //恢复条目
                $data = array('status' => 1);
                $this->editRow($model, $data, $map, array('success'=>'启用成功','error'=>'启用失败'));
                break;
            case 'delete'  : //假删除条目
                $data['status'] = -1;
                $this->editRow($model, $data, $map, array('success'=>'删除成功','error'=>'删除失败'));
                break;
            case 'restore' : //从假删除状态还原条目
                $data = array('status' => 1);
                $map  = array_merge(array('status' => -1), $map);
                $this->editRow($model, $data, $map, array('success'=>'恢复成功','error'=>'恢复失败'));
                break;
            case 'destory' : //真删除条目
                $result = D($model)->where($map)->delete();
                if($result){
                    $this->success('确认删除成功，不可恢复！');
                }else{
                    $this->error('确认删除失败');
                }
                break;
            default :
                $this->error('参数错误');
                break;
        }
    }

    /**
     * 回收站
     * @author jry <598821125@qq.com>
     */
    public function recycle($model = 3){
        if(!$model){
            $this->error('模型ID错误');
        }
        $map['status'] = array('eq', '-1');
        $current_model = D('Model')->getModelById($model);
        $volist = D($current_model['name'])->page(!empty($_GET["p"])?$_GET["p"]:1, C('ADMIN_PAGE_ROWS'))->where($map)->select();
        $page = new \Think\Page(D($current_model['name'])->where($map)->count(), C('ADMIN_PAGE_ROWS'));
        $this->assign('page', $page->show());
        $this->assign('volist', int_to_icon($volist));
        $this->assign('__MODEL_LIST__', D('Model')->getAllModel(array('id' => array('egt', '3'))));
        $this->assign('__CURRENT_MODEL_', $current_model);
        $this->meta_title = '回收站';
        $this->display();
    }

    /**
     * 对数据表中的单行或多行记录执行修改 GET参数id为数字或逗号分隔的数字
     * @param string $model 模型名称,供M函数使用的参数
     * @param array  $data  修改的数据
     * @param array  $map   查询时的where()方法的参数
     * @param array  $msg   执行正确和错误的消息 array('success'=>'','error'=>'', 'url'=>'','ajax'=>false)
     *                      url为跳转页面,ajax是否ajax方式(数字则为倒数计时秒数)
     * @author jry <598821125@qq.com>
     */
    final protected function editRow($model, $data, $map, $msg){
        $id = array_unique((array)I('id',0));
        $id = is_array($id) ? implode(',',$id) : $id;
        //如存在id字段，则加入该条件
        $fields = M($model)->getDbFields();
        if(in_array('id',$fields) && !empty($id)){
            $where = array_merge(array('id' => array('in', $id )) ,(array)$where);
        }
        $msg = array_merge(array('success'=>'操作成功！', 'error'=>'操作失败！', 'url'=>'' ,'ajax'=>IS_AJAX) , (array)$msg);
        if(M($model)->where($map)->save($data) !== false ) {
            $this->success($msg['success'], $msg['url'], $msg['ajax']);
        }else{
            $this->error($msg['error'], $msg['url'], $msg['ajax']);
        }
    }

    /**
     * 完全删除指定文件目录
     * @author jry <598821125@qq.com>
     */
    public function rmdirr($dirname = RUNTIME_PATH){
        $file = new \Org\Util\File();
        $result = $file->del_dir($dirname);
        if($result){
            $this->success("缓存清理成功");
        }else{
            $this->error("缓存清理失败");
        }
    }
}
